//
//  OtherListTableViewCell.m
//  MeterMart
//
//  Created by ENERGY on 2018/11/3.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "OtherListTableViewCell.h"
#import "FeatureCollectionViewCell.h"

@interface OtherListTableViewCell()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong)UIView * whiteView;
@property (nonatomic, strong)UIImageView * img;
@property (nonatomic, strong)UILabel * titleLab;
@property (nonatomic, strong)UILabel * levelLab;
@property (nonatomic, strong)UILabel * spendNumLab;
@property (nonatomic, strong)UILabel * locationLab;
@property (nonatomic, strong)UILabel * moneyLab;
@property (nonatomic, strong)UILabel * distanceLab;
@property (nonatomic, strong)UICollectionView * collcetionView;
@property (nonatomic, strong)NSMutableArray * itemCellArr;
@property (nonatomic, strong)NSString * cellID;
@end
@implementation OtherListTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = getColor(mainColor);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        __weak typeof (self)weakSelf = self;
        [self.contentView addSubview:self.whiteView];
        [self.contentView addSubview:self.img];
        [self.contentView addSubview:self.titleLab];
        [self.contentView addSubview:self.levelLab];
        [self.contentView addSubview:self.spendNumLab];
        [self.contentView addSubview:self.locationLab];
        [self.contentView addSubview:self.moneyLab];
        [self.contentView addSubview:self.distanceLab];
        [self.contentView addSubview:self.collcetionView];
        [self.whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.contentView.mas_left).mas_offset(6/WIDTH_6_SCALE);
            make.right.equalTo(weakSelf.contentView.mas_right).mas_offset(-6/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.contentView.mas_top).mas_offset(6/WIDTH_6_SCALE);
            make.height.mas_offset(160/WIDTH_6_SCALE);
        }];
        [self.img mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.whiteView.mas_left).mas_offset(12/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.whiteView.mas_top).mas_offset(20/WIDTH_6_SCALE);
            make.height.mas_offset(91/WIDTH_6_SCALE);
            make.width.mas_offset(105/WIDTH_6_SCALE);
        }];
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.img.mas_right).mas_offset(12/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.img.mas_top).mas_offset(4/WIDTH_6_SCALE);
        }];
        [self.distanceLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.whiteView.mas_right).mas_offset(-15/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.titleLab);
        }];
        [self.levelLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.titleLab);
            make.top.equalTo(weakSelf.distanceLab.mas_bottom).mas_offset(9/WIDTH_6_SCALE);
        }];
        [self.spendNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(weakSelf.levelLab);
            make.left.equalTo(weakSelf.levelLab.mas_right).mas_offset(25/WIDTH_6_SCALE);
        }];
        [self.locationLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.titleLab);
            make.top.equalTo(weakSelf.spendNumLab.mas_bottom).mas_offset(9/WIDTH_6_SCALE);
        }];
        [self.moneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.titleLab);
            make.top.equalTo(weakSelf.locationLab.mas_bottom).mas_offset(8/WIDTH_6_SCALE);
        }];
        [self.collcetionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf.contentView);
            make.height.mas_offset(34/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.img.mas_bottom).mas_offset(7/WIDTH_6_SCALE);
        }];
    }return self;
}
- (NSMutableArray *)itemCellArr{
    if (!_itemCellArr) {
        _itemCellArr = @[].mutableCopy;
    }return _itemCellArr;
}
- (UIView *)whiteView{
    if (!_whiteView) {
        _whiteView = [[UIView alloc] init];
        _whiteView.backgroundColor = [UIColor whiteColor];
    }return _whiteView;
}
- (UIImageView *)img{
    if (!_img) {
        _img = [[UIImageView alloc] init];
    }return _img;
}
- (UILabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.font = DEF_FontSize_13;
        _titleLab.textColor = [UIColor blackColor];
        _titleLab.textAlignment = NSTextAlignmentLeft;
        _titleLab.text = @"酒店名";
    }return _titleLab;
}
- (UILabel *)levelLab{
    if (!_levelLab) {
        _levelLab = [[UILabel alloc] init];
        _levelLab.font = DEF_FontSize_10;
        _levelLab.textColor = getColor(textGrayColor);
        _levelLab.textAlignment = NSTextAlignmentLeft;
        _levelLab.text = @"很好:4.8分";
    }return _levelLab;
}
- (UILabel *)spendNumLab{
    if (!_spendNumLab) {
        _spendNumLab = [[UILabel alloc] init];
        _spendNumLab.font = DEF_FontSize_10;
        _spendNumLab.textColor = getColor(textGrayColor);
        _spendNumLab.textAlignment = NSTextAlignmentLeft;
        _spendNumLab.text = @"3人消费";
    }return _spendNumLab;
}
- (UILabel *)locationLab{
    if (!_locationLab) {
        _locationLab = [[UILabel alloc] init];
        _locationLab.font = DEF_FontSize_10;
        _locationLab.text = @"正良 沈阳工程学院";
        _locationLab.textColor = getColor(textGrayColor);
        _locationLab.textAlignment = NSTextAlignmentLeft;
    }return _locationLab;
}
- (UILabel *)moneyLab{
    if (!_moneyLab) {
        _moneyLab = [[UILabel alloc] init];
        _moneyLab.font = [UIFont systemFontOfSize:16.0];
        _moneyLab.text = @"￥--";
        _moneyLab.textColor = getColor(mainRedColor);
        _moneyLab.textAlignment = NSTextAlignmentLeft;
    }return _moneyLab;
}
- (UILabel *)distanceLab{
    if (!_distanceLab) {
        _distanceLab = [[UILabel alloc] init];
        _distanceLab.font = DEF_FontSize_10;
        _distanceLab.text = @"1.2km";
        _distanceLab.textColor = getColor(textGrayColor);
        _distanceLab.textAlignment = NSTextAlignmentLeft;
    }return _distanceLab;
}
- (UICollectionView *)collcetionView{
    if (!_collcetionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
        layout.estimatedItemSize = CGSizeMake(60/WIDTH_6_SCALE, 30/WIDTH_6_SCALE);// layout约束这边必须要用estimatedItemSize才能实现自适应,使用itemSzie无效
        layout.scrollDirection =  UICollectionViewScrollDirectionHorizontal;
        layout.sectionInset = UIEdgeInsetsMake(0/WIDTH_6_SCALE, 12/WIDTH_6_SCALE, 0/WIDTH_6_SCALE, 0/WIDTH_6_SCALE);
        // 设置 最小列间距
        layout. minimumInteritemSpacing  = 14/WIDTH_6_SCALE;
        _collcetionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collcetionView.backgroundColor = [UIColor whiteColor];
        _collcetionView.delegate = self;
        _collcetionView.dataSource = self;
        [_collcetionView registerClass:[FeatureCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([FeatureCollectionViewCell class])];
    }return _collcetionView;
}
#pragma mark - UICollectionViewDelegate和UICollectionViewDataSource
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FeatureCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([FeatureCollectionViewCell class]) forIndexPath:indexPath];
    if (indexPath.row % 3 == 0) {
        cell.bgView.layer.borderColor = [UIColor orangeColor].CGColor;
        cell.contentLab.textColor = [UIColor orangeColor];
    }else if (indexPath.row % 3 == 1){
        cell.bgView.layer.borderColor = getColor(@"005de9").CGColor;
        cell.contentLab.textColor = getColor(@"005de9");
    }else{
        cell.bgView.layer.borderColor = getColor(@"ea07b1").CGColor;
        cell.contentLab.textColor = getColor(@"ea07b1");
    }
    cell.contentLab.text = self.itemCellArr[indexPath.row];
    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.itemCellArr.count;
}
- (void)setModel:(NSDictionary *)model{
    _model = model;
    [self.itemCellArr removeAllObjects];
    [self.itemCellArr addObjectsFromArray:model[@"introduce"]];
    self.levelLab.text = [NSString stringWithFormat:@"很好:%@", model[@"score"]];
    self.locationLab.text = [NSString stringWithFormat:@"%@", model[@"iocation"]];
    self.cellID = model[@"typeid_goods"];
    [self.img sd_setImageWithURL:[NSURL URLWithString:model[@"cover_image"]] placeholderImage:[UIImage imageNamed:@"YimiPlaceHolder"]];
    self.distanceLab.text = model[@"distant"];
    self.moneyLab.text = [NSString stringWithFormat:@"￥%@起", model[@"consumption"]];
    self.titleLab.text = model[@"name"];
    self.spendNumLab.text = [NSString stringWithFormat:@"%@人消费", model[@"num"]];
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.moneyLab.text];
    NSRange range1 = [[str string] rangeOfString:@"￥"];
    [str addAttribute:NSFontAttributeName value:[UIFont fontSize:10.0] range:range1];
    NSRange range3 = [[str string] rangeOfString:@"起"];
    [str addAttribute:NSFontAttributeName value:[UIFont fontSize:10.0] range:range3];
    [str addAttribute:NSForegroundColorAttributeName value:getColor(textGrayColor) range:range3];
    self.moneyLab.attributedText = str;
}

@end
